package com.hafu.dao;

import com.hafu.model.KhInfo;
import com.hafu.util.DbUtil;
import com.hafu.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class KhInfoDao {
    public Boolean addKhInfo(KhInfo kh) {
        String sql = "insert into khinfo values(null,?,?)";
        Connection conn = DbUtil.getConn();

        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, kh.getName());
            preparedStatement.setString(2, kh.getTel());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("添加成功");
                return true;
            } else return false;


        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;

    }

    public Boolean deleteKhInfo(int id) {
        String sql = "delete from khinfo where id=?";
        Connection conn = DbUtil.getConn();
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("删除成功");
                return true;
            } else return false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();

        }
        return false;
    }

    public ArrayList<KhInfo> getKhInfoByName(KhInfo kh) {
        ArrayList<KhInfo> list = new ArrayList<>();
        Connection conn = DbUtil.getConn();
        String sql = "select * from khinfo";
        if (!StringUtil.isEmpty(kh.getName())) {
            sql += " where name like '%" + kh.getName() + "%'";
        }
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                KhInfo khInfo = new KhInfo();
                khInfo.setId(resultSet.getInt("id"));
                khInfo.setName(resultSet.getString("name"));
                khInfo.setTel(resultSet.getString("tel"));
                list.add(khInfo);
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return list;


    }
    public Boolean updateKhInfo(KhInfo kh) {
        String sql = "update khinfo set name=?,tel=? where id=?";
        Connection conn = DbUtil.getConn();
        try{
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,kh.getName());
            preparedStatement.setString(2,kh.getTel());
            preparedStatement.setInt(3,kh.getId());
            int i = preparedStatement.executeUpdate();
            if (i>0){
                System.out.println("修改成功");
                return true;
            }else return false;

        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return false;
    }
}
